/*
** ADOBE SYSTEMS INCORPORATED
** Copyright 2012 Adobe Systems Incorporated
** All Rights Reserved.
**
** NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
** terms of the Adobe license agreement accompanying it.  If you have received this file from a
** source other than Adobe, then your use, modification, or distribution of it requires the prior
** written permission of Adobe.
*/

#include "Asteroid.h"
#include "Game.h"
#include "Ship.h"
#include <MathUtils.h>

namespace SpaceTriangle
{
	Asteroid::Asteroid(flash::display3D::Context3D _ctx3d, Game *_game, vec3 _pos, vec3 _speed) :
		Actor(_ctx3d),
		game(_game)
	{
		speed = _speed;
		pos = _pos;

		i3dbuffer = ctx3d->createIndexBuffer(18);
	    int indicies[] = {
	    	0,2,1,
	    	0,2,3,
	    	0,3,4,
	    	0,4,5,
	    	0,5,6,
	    	0,6,1
	    };
	    i3dbuffer->uploadFromByteArray(internal::get_ram(), (int)&indicies[0], 0, 18, (void*)&indicies[0]);

	    v3dbuffer = ctx3d->createVertexBuffer(7, 3);
	    float verticies[] = {
	    	0.0f,   0.0f, 0.0f,    //center
			-5.0f,   10.0f, 0.0f,    // left top
			5.0f,   10.0f, 0.0f,    // right top
			10.0f,   0.0f, 0.0f,    // right
			5.0f,   10.0f, 0.0f,    // right bottom
			-5.0f,  -10.0f, 0.0f,    // left bottom
			-10.0f,   0.0f, 0.0f,     // left
	    };
	    v3dbuffer->uploadFromByteArray(internal::get_ram(), (int)&verticies[0], 0, 7, (void*)&verticies[0]);

	    colorVector = internal::new_Vector_Number();
	    colorVector[0] = internal::new_Number(1.0);
	    colorVector[1] = internal::new_Number(0.5);
	    colorVector[2] = internal::new_Number(0.5);
	    colorVector[3] = internal::new_Number(0.5);

	    objectToWorld = flash::geom::Matrix3D::_new();
	}

	void Asteroid::tick()
	{
		pos += speed;

		if(pos.x < -game->width / 2.0)
			pos.x = game->width / 2.0;
		else if(pos.x > game->width / 2.0)
			pos.x = -game->width / 2.0;
		
		if(pos.y < -game->height / 2.0)
			pos.y = game->height / 2.0;
		else if(pos.y > game->height / 2.0)
			pos.y = -game->height / 2.0;
	}

	void Asteroid::render()
	{
		objectToWorld->identity();
		//objectToWorld->appendRotation(rads * (180.0/M_PI), flash::geom::Vector3D::Z_AXIS, internal::_null);
		objectToWorld->appendTranslation(pos.x, pos.y, pos.z);

		ctx3d->setProgramConstantsFromMatrix(flash::display3D::Context3DProgramType::VERTEX, 4, objectToWorld, true);
		ctx3d->setProgramConstantsFromVector(flash::display3D::Context3DProgramType::FRAGMENT, 0, colorVector, -1);
		ctx3d->setVertexBufferAt(0, v3dbuffer, 0, flash::display3D::Context3DVertexBufferFormat::FLOAT_3);
		ctx3d->drawTriangles(i3dbuffer, 0, -1);
	}
}